package net.shenru.ui.map;

import net.shenru.R;
import net.shenru.encogs.EncogTask;
import net.shenru.lbs.BaiduMapManager;
import net.shenru.lbs.SrLocationManager;
import net.shenru.modle.map.SrAddress;
import net.shenru.os.utlis.SrLog;
import net.shenru.os.utlis.SrToast;
import net.shenru.ui.encog.EncogBusActivity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.RouteOverlay;

/**
 * 百度地图
 */
public class BaiduMapActivity extends MapActivity implements LocationListener, OnClickListener {

	private static final String TAG = BaiduMapActivity.class.getSimpleName();

	private BaiduMapManager mBMapManager;
	private MyLocationOverlay mMyOverlay;
	private MKSearch mSearch;

	private MapView mMapView;
	private Button mBusView;
	private Button mEncogView;
	private EditText mBusNameView;
	protected SrAddress mSrAddress;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		// 设置布局
		setContentView(R.layout.map_baidu_activity);

		findView();
		setListener();

		mBMapManager = BaiduMapManager.getInstance(getApplicationContext(), null);
		mBMapManager.start();
		super.initMapActivity(mBMapManager.getBMapManager());
		mSearch = new MKSearch();
		mSearch.init(mBMapManager.getBMapManager(), new BusSearchLinstener());
		MKSearch.setPoiPageCapacity(50);

		mMapView.setBuiltInZoomControls(true);
		mMapView.setDrawOverlayWhenZooming(true);

		mMyOverlay = new MyLocationOverlay(this, mMapView);
		mMapView.getOverlays().add(mMyOverlay);

	}

	private void setListener() {
		/** 查询公交 */
		mBusView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String busName = mBusNameView.getText().toString().trim();
				if (TextUtils.isEmpty(busName)) {
					SrToast.show(getApplication(), "您到哪里未输入");
				} else {
					mSrAddress = SrLocationManager.getInstance(getApplicationContext()).getLastSrAddress();
					mSearch.poiSearchInCity(mSrAddress.getCity(), busName);
				}
			}
		});
	}

	private void findView() {
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBusView = (Button) findViewById(R.id.but_bus);
		mBusNameView = (EditText) findViewById(R.id.edt_busname);
		mEncogView = (Button) findViewById(R.id.but_encog);
		mEncogView.setOnClickListener(this);
		mBusNameView.setText("944");
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onResume() {
		mMyOverlay.enableMyLocation();
		mMyOverlay.enableCompass();

		mBMapManager.getLocationManager().requestLocationUpdates(this);
		mBMapManager.start();

		super.onStart();
	}

	@Override
	protected void onPause() {
		mMyOverlay.disableMyLocation();
		mMyOverlay.disableCompass();

		mBMapManager.getLocationManager().removeUpdates(this);
		mBMapManager.stop();

		super.onPause();
	}

	/**
	 * 位置改变
	 */
	@Override
	public void onLocationChanged(Location location) {
		if (location != null) {
			GeoPoint pt = new GeoPoint((int) (location.getLatitude() * 1e6), (int) (location.getLongitude() * 1e6));
			mMapView.getController().animateTo(pt);
		}
	}

	private class BusSearchLinstener implements MKSearchListener {

		@Override
		public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
			SrLog.i(TAG, "SrSearchLinstener");
		}

		@Override
		public void onGetBusDetailResult(MKBusLineResult result, int arg1) {
			// TODO Auto-generated method stub
			SrLog.i(TAG, "onGetBusDetailResult error:"+ arg1);
			if (result != null) {
				RouteOverlay overlay = new RouteOverlay(BaiduMapActivity.this, mMapView);
				overlay.setData(result.getBusRoute());
				mMapView.getOverlays().clear();
				mMapView.getOverlays().add(overlay);
				mMapView.invalidate();

				mMapView.getController().animateTo(result.getBusRoute().getStart());
			} else {
				SrLog.i(TAG, "搜索结果为空");
			}
		}

		@Override
		public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			SrLog.i(TAG, "onGetDrivingRouteResult");
		}

		@Override
		public void onGetPoiResult(MKPoiResult result, int type, int error) {
			// TODO Auto-generated method stub
			SrLog.i(TAG, "onGetPoiResult");
			// 错误号可参考MKEvent中的定义
			if (error != 0 || result == null) {
				SrToast.show(getApplicationContext(), "没有搜索到结果");
			} else {
				// int pois = result.getNumPois();
				// MKPoiInfo poiInfo = null;
				// for (int i = 0; i < pois; i++) {
				// poiInfo = result.getPoi(i);
				// if (poiInfo.ePoiType == 2) {
				// mSearch.busLineSearch(mSrAddress.getCity(), poiInfo.uid);
				// } else {
				// poiInfo = null;
				// }
				// }

				boolean isFindPoi = false;
				SrLog.i(TAG, "getNumPages count:" + result.getNumPois());
				
				if (poiPage == -1) {
					SrLog.i(TAG, "getNumPages size:" + result.getNumPages());
					SrLog.i(TAG, "getCityListNum size:" + result.getCityListNum());
					poiPage =  result.getNumPages() / 50;
				}
				for (MKPoiInfo poi : result.getAllPoi()) {
//					SrLog.i(TAG, "poi name:" + poi.name);
					if (poi.ePoiType == 2) {
						SrLog.i(TAG, "poi uid:" + poi.uid);
						isFindPoi = true;
						mSearch.busLineSearch(mSrAddress.getCity(), poi.uid);
					}
				}

				if (!isFindPoi) {
					SrToast.show(getApplicationContext(), "没有找到对应的站");

				}
				SearchNextPagePoi();
			}
		}

		private int poiPage = -1;
		private int poiIndex = 0;

		private synchronized void SearchNextPagePoi() {
			// TODO Auto-generated method stub
			if (poiIndex < poiPage) {
				poiIndex++;
				mSearch.goToPoiPage(poiIndex);
				SrLog.d(TAG, "poiIndex:" + poiIndex);
			}
		}

		@Override
		public void onGetRGCShareUrlResult(String arg0, int arg1) {
			// TODO Auto-generated method stub
			SrLog.i(TAG, "onGetRGCShareUrlResult");
		}

		@Override
		public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
			// TODO Auto-generated method stub
			SrLog.i(TAG, "onGetSuggestionResult");
		}

		@Override
		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			SrLog.i(TAG, "onGetTransitRouteResult");
		}

		@Override
		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			SrLog.i(TAG, "onGetWalkingRouteResult");
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.but_encog:
			openEncogs(v);
			break;
		}

	}

	private void openEncogs(View v) {
		Intent it = new Intent(this, EncogBusActivity.class);
		startActivity(it);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}